<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="空开编码" prop="airNum">
        <el-input
          v-model="queryParams.airNum"
          placeholder="请输入部件编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="线径" prop="diameter">-->
        <!--<el-input-->
          <!--v-model="queryParams.diameter"-->
          <!--placeholder="请输入线径"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="状态" prop="status">-->
        <!--<el-select-->
          <!--clearable-->
          <!--v-model="queryParams.status"-->
          <!--placeholder="请选择状态"-->
        <!--&gt;-->
          <!--<el-option-->
            <!--v-for="item in workState"-->
            <!--:key="item.dictValue"-->
            <!--:label="item.dictLabel"-->
            <!--:value="item.dictValue"-->
          <!--&gt;-->
          <!--</el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="企业" prop="en">
        <el-input
          v-model="queryParams.en"
          placeholder="请输入企业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyTransPartArrs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyTransPartArrs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyTransPartArrs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyTransPartArrs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nyTransPartArrsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="序号" align="center" prop="attrId" />-->
      <!--<el-table-column label="变压器编号" align="center" prop="transId" />-->
      <el-table-column label="变压器编码" align="center" prop="transNum" />
      <el-table-column label="变压器名称" align="center" prop="transName" />
      <!--<el-table-column label="低控编号" align="center" prop="partId" />-->
      <!--<el-table-column label="低控编码" align="center" prop="partNum" />-->
      <!--<el-table-column label="低控名称" align="center" prop="partName" />-->
      <!--<el-table-column label="空开编码" align="center" prop="airNum" />-->
      <el-table-column label="空开名称" align="center" prop="airName" />
      <el-table-column label="规格型号(A)" align="center" prop="modal" />
      <!--<el-table-column label="单位" align="center" prop="unit" />-->
      <!--<el-table-column label="额定容量" align="center" prop="allowLoad" />-->
      <el-table-column label="线径(mm)" align="center" prop="diameter" />
      <el-table-column label="企业名称" align="center" prop="en" />
      <el-table-column label="申报容量" align="center" prop="enAllow" />
      <!--<el-table-column label="状态" align="center" prop="stateNew" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyTransPartArrs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyTransPartArrs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改变压器部件属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" @opened ="select" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="变压器编码"  prop="transId">
          <el-select v-model="form.transId" @change="select" placeholder="请选择变压器编码">
            <el-option
              v-for="item in transIdList"
              :key="item.id"
              :label="item.num"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="空开编码" prop="airId">
          <el-select v-model="form.airId" placeholder="请选择空开编码">
            <el-option
              v-for="item in partList"
              :key="item.id"
              :label="item.num"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="规格型号(A)" prop="modal">-->
          <!--<el-input v-model="form.modal" placeholder="请输入规格型号" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="单位" prop="unit">-->
          <!--<el-select v-model="form.unit" placeholder="请选择单位">-->
            <!--<el-option-->
              <!--v-for="item in unitList"-->
              <!--:key="item.dictValue"-->
              <!--:label="item.dictLabel"-->
              <!--:value="item.dictValue"-->
            <!--/>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="容量" prop="allowLoad">-->
          <!--<el-input v-model="form.allowLoad" type="number" placeholder="请输入空开容量" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="线径(mm)" prop="diameter">-->
          <!--<el-input v-model="form.diameter" placeholder="请输入线径" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="状态" prop="status">-->
          <!--<el-select-->
            <!--clearable-->
            <!--v-model="form.status"-->
            <!--placeholder="请选择状态"-->
          <!--&gt;-->
            <!--<el-option-->
              <!--v-for="item in workState"-->
              <!--:key="item.dictValue"-->
              <!--:label="item.dictLabel"-->
              <!--:value="item.dictValue"-->
            <!--&gt;-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <!--<el-form-item label="企业" prop="enterpriseId">-->
          <!--<el-select            clearable-->
            <!--v-model="form.enterpriseId" filterable-->
            <!--placeholder="请选择企业"-->
            <!--size="small"-->
          <!--&gt;-->
            <!--<el-option-->
              <!--v-for="item in qyEnterpriseList"-->
              <!--:key="item.id"-->
              <!--:label="item.enterpriseName"-->
              <!--:value="item.id"-->
            <!--&gt;-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-divider content-position="center">企业申报容量</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddNyEnteriseLoad">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteNyEnteriseLoad">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="nyEnteriseLoadList" :row-class-name="rowNyEnteriseLoadIndex" @selection-change="handleNyEnteriseLoadSelectionChange" ref="nyEnteriseLoad">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <!--<el-table-column label="企业名称" prop="entepriseId">-->
            <!--<template slot-scope="scope">-->
              <!--&lt;!&ndash;<el-input v-model="scope.row.entepriseId" placeholder="请输入企业id" />&ndash;&gt;-->
              <!--<el-select clearable v-model="scope.row.entepriseId" filterable placeholder="请选择企业" size="small">-->
              <!--<el-option v-for="item in qyEnterpriseList"-->
              <!--:key="item.id" :label="item.enterpriseName" :value="item.id">-->
              <!--</el-option>-->
              <!--</el-select>-->
            <!--</template>-->
          <!--</el-table-column>-->
          <el-table-column label="企业名称" prop="entepriseName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.entepriseName" placeholder="请输入企业名称" />
            </template>
          </el-table-column>
          <el-table-column label="申报容量" prop="approvalLoad">
            <template slot-scope="scope">
              <el-input v-model="scope.row.approvalLoad" placeholder="请输入企业申报容量" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNyTransPartArrs, getNyTransPartArrs, delNyTransPartArrs, addNyTransPartArrs, updateNyTransPartArrs } from "@/api/smartParks/energy/nyTransPartArrs";
import { listQyEnterprise } from "@/api/smartParks/merchants/merchantsCompanySettlein";
import { listData } from "@/api/system/dict/data"; //字典接口
import { listNyTrans } from "@/api/smartParks/energy/nyTrans";
import { listNyTransPart } from "@/api/smartParks/energy/nyTransPart";

export default {
  name: "NyTransPartArrs",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedNyEnteriseLoad: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 变压器部件属性表格数据
      nyTransPartArrsList: [],
      // ${subTable.functionName}表格数据
      nyEnteriseLoadList: [],
      qyEnterpriseList:[],
      //workState: [],
      transIdList:[],
      //unitList:[],
      partList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        partId: null,
        diameter: null,
        en: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        transId:[{required: true,message: "变压器编码不能为空", trigger: "change",}],
        partId:[{required: true,message: "低压控制柜编码不能为空", trigger: "change",}],
        airNum:[{required: true,message: "空开编码不能为空", trigger: "change",}],
        // modal:[{required: true,message: "规格型号不能为空", trigger: "change",}],
      }
    };
  },
  created() {
    this.getList();
    listQyEnterprise().then((response) => {
      this.qyEnterpriseList = response.rows;
    });
    // listData({ dictType: 'air_use_state' }).then((response) => {
    //   this.workState = response.rows
    // });
    // listData({ dictType: 'unit_type' }).then((response) => {
    //   this.unitList = response.rows
    // });
    listNyTrans().then(response=>{
      response.rows.forEach((item) => {
        item.num = item.num + "==" + item.name;
      });
      this.transIdList = response.rows;
    });
  },
  methods: {
    /** 查询变压器部件属性列表 */
    getList() {
      this.loading = true;
      listNyTransPartArrs(this.queryParams).then(response => {
          // response.rows.forEach((item) => {
          //   this.workState.forEach((itemType) => {
          //     if (itemType.dictValue === item.status) {
          //       item.stateNew = itemType.dictLabel
          //     }
          //   });
          // });
        this.nyTransPartArrsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attrId: null,
        transId: null,
        partId: null,
        airNum: null,
        modal: null,
        unit: null,
        amount: null,
        diameter: null,
        curValue: null,
        status: "0",
        en: null,
        enAllow: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.nyEnteriseLoadList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attrId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加空开申报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attrId = row.attrId || this.ids
      getNyTransPartArrs(attrId).then(response => {
        this.form = response.data;
        this.nyEnteriseLoadList = response.data.nyEnteriseLoadList;
        this.open = true;
        this.title = "修改变压器空开";
      });
    },
    select(transId){
      const self=this;
      listNyTransPart({transId:transId,type:'空开'}).then((response) =>{
        response.rows.forEach((item) => {
          item.num = item.name + "==" + item.modal;
        });
        self.partList = response.rows;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.nyEnteriseLoadList = this.nyEnteriseLoadList;
          if (this.form.attrId != null) {
            updateNyTransPartArrs(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNyTransPartArrs(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attrIds = row.attrId || this.ids;
      this.$confirm('是否确认删除变压器部件属性编号为"' + attrIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNyTransPartArrs(attrIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** ${subTable.functionName}序号 */
    rowNyEnteriseLoadIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddNyEnteriseLoad() {
      let obj = {};
      obj.entepriseId = "";
      obj.entepriseName = "";
      obj.approvalLoad = "";
      this.nyEnteriseLoadList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteNyEnteriseLoad() {
      if (this.checkedNyEnteriseLoad.length == 0) {
        this.$alert("请先选择要删除的${subTable.functionName}数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.nyEnteriseLoadList.splice(this.checkedNyEnteriseLoad[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleNyEnteriseLoadSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.nyEnteriseLoad.clearSelection();
        this.$refs.nyEnteriseLoad.toggleRowSelection(selection.pop());
      } else {
        this.checkedNyEnteriseLoad = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('energy/nyTransPartArrs/export', {
        ...this.queryParams
      }, `energy_nyTransPartArrs.xlsx`)
    }
  }
};
</script>
<style scoped lang='scss'>
  input[type="number"]{
    -moz-appearance: textfield;
  }
</style>
